<%-- 
    Document   : insertupdate
    Created on : Nov 8, 2013, 8:40:00 PM
    Author     : Shin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.*" %>
<%@page import="model.UserBean" %>
<%@page import="database.DBClass"%>
<%@page import="validation.ValidateForm"%>
<%@ page import ="java.sql.*" %>

<%
    UserBean bean;
    bean = new UserBean();
    //get request from servlet if data is invalid
    if (request.getAttribute("error") != null) {
        bean = (UserBean) request.getAttribute("error");
    }
    if (request.getAttribute("updateuser") != null) {
        bean = (UserBean) request.getAttribute("updateuser");
    }

    if (session.getAttribute("account") == null) {
        RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
        rd.forward(request, response);
    } else if (!session.getAttribute("account").equals("admin")) {
        response.sendRedirect("view.jsp");
    }
    DBClass dbobject = new DBClass();
    List list = dbobject.getProvinsi();

%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="css/bootstrap.min.css" rel="stylesheet"> 
        <link href="css/bootstrap-responsive.min.css" rel="stylesheet">
        <title>JSP Page</title>
    </head>
    <body>
        <script language="javascript" type="text/javascript">
            var xmlHttp
            var xmlHttp
            function showKabkota(str) {
                if (typeof XMLHttpRequest != "undefined") {
                    xmlHttp = new XMLHttpRequest();
                }
                else if (window.ActiveXObject) {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                if (xmlHttp == null) {
                    alert("Browser does not support XMLHTTP Request")
                    return;
                }
                var url = "kabkota.jsp";
                url += "?count=" + str;
                xmlHttp.onreadystatechange = stateChange;
                xmlHttp.open("GET", url, true);
                xmlHttp.send(null);
            }

            function stateChange() {
                if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
                    document.getElementById("kabkota").innerHTML = xmlHttp.responseText
                }
            }

            function showKecamatan(str) {
                if (typeof XMLHttpRequest != "undefined") {
                    xmlHttp = new XMLHttpRequest();
                }
                else if (window.ActiveXObject) {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                if (xmlHttp == null) {
                    alert("Browser does not support XMLHTTP Request")
                    return;
                }
                var url = "kecamatan.jsp";
                url += "?count=" + str;
                xmlHttp.onreadystatechange = stateChange1;
                xmlHttp.open("GET", url, true);
                xmlHttp.send(null);
            }
            function stateChange1() {
                if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
                    document.getElementById("kecamatan").innerHTML = xmlHttp.responseText
                }
            }

            function showDesa(str) {
                if (typeof XMLHttpRequest != "undefined") {
                    xmlHttp = new XMLHttpRequest();
                }
                else if (window.ActiveXObject) {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                if (xmlHttp == null) {
                    alert("Browser does not support XMLHTTP Request")
                    return;
                }
                var url = "desa.jsp";
                url += "?count=" + str;
                xmlHttp.onreadystatechange = stateChange2;
                xmlHttp.open("GET", url, true);
                xmlHttp.send(null);
            }
            function stateChange2() {
                if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
                    document.getElementById("desa").innerHTML = xmlHttp.responseText
                }
            }
        </script> 
        <form method="post" action="ControllerServlet">
            <CENTER>
                <TABLE border="0"width="600px">
                    <TR>
                        <TD width="150px">Name</TD>
                        <TD>:
                            <INPUT TYPE="text" NAME="userName" value="<%=bean.getUserName()%>" <%=request.getAttribute("updateuser") != null ? "readonly" : ""%>>
                        </TD>
                        <TD width="350px">
                            <font color="red"><%=bean.getUserNameError()%> &nbsp;</font>
                        </TD>
                    </TR>
                    <TR>
                        <TD width="150px">Date Of Birth</TD>
                        <TD>:
                            <INPUT TYPE="text" NAME="dateOfBirth" value="<%=bean.getDateOfBirth()%>">
                        </TD>
                        <TD>
                            <font color="red"><%=bean.getDateOfBirthError()%> </font>
                        </TD>
                    </TR>
                    <TR>
                        <TD width="150px">E-Mail</TD>
                        <TD>:
                            <INPUT TYPE="text" NAME="email" value="<%=bean.getEmail()%>">
                        </TD>
                        <TD>
                            <font color="red"><%=bean.getEmailError()%> </font>
                        </TD>
                    </TR>
                    <TR>
                        <TD width="150px">Phone no</TD>
                        <TD>:
                            <INPUT TYPE="text" NAME="phoneNo" value="<%=bean.getPhoneNo()%>">
                        </TD>
                        <TD>
                            <font color="red"><%=bean.getPhoneNoError()%> </font>
                        </TD>
                    </TR>
                    <TR>
                        <TD width="150px">password</TD>
                        <TD>:
                            <INPUT TYPE="password" NAME="password" value="<%=bean.getPassword()%>" >
                        </TD>
                        <TD>

                        </TD>
                    </TR>
                    <tr>
                        <td>Provinsi</td>
                        <td>:
                            <script type="text/javascript" src="js/ajax_kota.js"></script>
                            <select name="prop" id="prop" onchange="showKabkota(this.value)">
                                <option value="">Pilih Provinsi</option>


                                <%
                                    if (list != null) {
                                        for (int i = 0; i < list.size(); i++) {
                                            UserBean ubean = (UserBean) list.get(i);

                                %>
                                <option value="<%=ubean.getProvinsiid()%>"><%=ubean.getProvinsi()%></option>
                                <%

                                        }
                                    }
                                %>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>Kota/Kab</td>
                        <td id='kabkota'>:<select name='kabkota' >  
                                <option value='-1'></option>  
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>Kecamatan:</td>
                        <td id='kecamatan'>:<select name='kecamatan' >  
                                <option value='-1'></option>  
                            </select>   
                        </td>
                    </tr>
                    <tr>
                        <td>Kelurahan/Desa:</td>
                        <td id='desa'>:<select name='desa' >  
                                <option value='-1'></option>  
                            </select>   
                        </td>

                    </tr>
                    <TR>
                        <TD colspan="2" align="center">
                            <INPUT TYPE="submit" value="<%=bean.getAction()%>" name="action">
                        </TD>
                        <TD>
                            &nbsp;
                        </TD>
                    </TR>
                </TABLE>
            </CENTER>
        </form>
    </body>
</html>
